package com.eqdd.yiqidian.ui.gongneng.renshi.yuangongdangan;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.EnterComCheckActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ComCheckListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

@DealCode(codes = {CodeRule.SHENGPI_HR_RUZHI})
public class EnterComCheckActivity extends BaseActivity {
    private EnterComCheckActivityCustom dataBinding;
    private SlimAdapterEx slimAdapterEx;

    @Subscribe(codes = {CodeRule.SHENGPI_HR_RUZHI},threadMode = ThreadMode.MAIN)
    public void refreshCode() {

    }

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_enter_com_check);
        initTopTitleBar(View.VISIBLE, "入住审核");
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_26, new SlimInjector<ComCheckListResultBean>() {
            @Override
            public void onInject(ComCheckListResultBean data, IViewInjector injector) {
                injector.text(R.id.tv_upper_content, data.getUname())
                        .text(R.id.tv_down_content, data.getDepartName() + " " + data.getPostName())
                        .with(R.id.iv_head, v -> {
                            ImageUtil.setCircleImage(data.getUiphoto(), (ImageView) v);
                        });

            }
        }).attachTo(dataBinding.recyclerView);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("ComCheckListResultBean", (ComCheckListResultBean) slimAdapterEx.getData().get(position));
                            startActivityForResult(EnterComCheckInfoActivity.class, bundle, Config.ENTER_COMCHECK_INFO);
                        }
                );
    }

    @Override
    public void setView() {

        OkGo.<HttpResult<List<ComCheckListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.COM_CHECK_LIST)
                .params("userGuid", user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<ComCheckListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<ComCheckListResultBean>>> response) {
                        HttpResult<List<ComCheckListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.CHANGE) {
            switch (requestCode) {
                case Config.ENTER_COMCHECK_INFO:
                    setView();
                    break;
            }
        }
    }
}
